package com.game.states;

import com.game.objects.Environment;
import com.game.objects.Player;
import com.game.system.FlightHandler;
import com.game.system.Game;
import com.jme.bounding.BoundingSphere;
import com.jme.input.InputHandler;
import com.jme.light.DirectionalLight;
import com.jme.light.SpotLight;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;
import com.jme.scene.lod.AreaClodMesh;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;
import com.jme.util.LoggingSystem;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.DebugGameState;

public class GameLogicState extends BasicGameState{
	private InputHandler input;
	private Player player;
	private Environment env;
	public GameLogicState() {
		super("Game Logic");
		Game.setGameLogic(this);
		createLighting();
		createEnvrionment();
		createPlayer();
		createInputHandler();
	    rootNode.updateRenderState();
	    DisplaySystem.getSystemProvider().getTimer().reset();
	}
	private void createEnvrionment(){
		env = new Environment();
		rootNode.attachChild(env.getEnvironment());
		rootNode.attachChild(env.getSkyBox());
	}
	private void createPlayer() {
		player = new Player();
	    rootNode.attachChild(player.getPlayer());
	    rootNode.attachChild(player.getCross()[0]);
	    rootNode.attachChild(player.getCross()[1]);
	    env.setPlayer(player);
	}
	private void createLighting() {
		LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
		lightState.setEnabled(true);
		SpotLight light = new SpotLight();
		light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
		light.setAmbient(new ColorRGBA(.8f, .8f, .8f, 1f));
		light.setLocation(new Vector3f(10, 10, 20));
		light.setAngle(180);
		light.setExponent(128);
		light.setEnabled(true);
		lightState.attach(light);
		rootNode.setRenderState(lightState);
	}
	private void createInputHandler() {
		input = new FlightHandler(player, env);
	}
	public void update(float tpf){
		//System.out.println(DisplaySystem.getSystemProvider().getTimer().getFrameRate());
		player.update();
		input.update(tpf);
		env.update(tpf);
		super.update(tpf);
	}
	public void cleanup() {
		env.cleanup();
		System.exit(0);
	}
	
	public Player getPlayer(){
		return player;
	}
	public Environment getEnvironment(){
		return env;
	}
}